package com.xn.app.muying.fragment;

import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.xn.app.muying.R;
import com.xn.app.muying.activity.WebActivity;
import com.xn.app.muying.util.APIMethod;
import com.xn.app.muying.util.MyAlertDialog;
import com.xn.app.muying.util.MyApplication;
import com.xn.app.muying.util.NetworkUtil;
import com.xn.app.muying.util.ToastUtil;
import com.zhuge.analysis.stat.ZhugeSDK;

/**
 * 商城
 * Created by gaoge on 2017/8/10.
 */

public class ShopFragment extends Fragment {

    //显示数据的webview
    private static WebView webview=null;
    //加载框
    AlertDialog dialog;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.shop_fragment, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        webview= (WebView) view.findViewById(R.id.webview);
        //功能实现
        init();
    }

    private void init() {
        //显示数据
        if (NetworkUtil.isNetworkAvailable(getActivity())) {
            dialog = new MyAlertDialog().ShowDialog(getActivity(), R.layout.dialog_loading);
            dialog.setCancelable(true);
            webview.setVisibility(WebView.VISIBLE);
            WebSettings mWebSettings = webview.getSettings();
            mWebSettings.setJavaScriptEnabled(true);
            mWebSettings.setLoadWithOverviewMode(true);
            mWebSettings.setUseWideViewPort(true);
            mWebSettings.setDefaultTextEncodingName("GBK");
            mWebSettings.setLoadsImagesAutomatically(true);
            webview.loadUrl(APIMethod.WeiDian);
        } else {
            ToastUtil.showToast(getActivity(), R.string.no_network_hint);
        }

        //web监听
        webview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO Auto-generated method stub
                if (url.contains("alipays://platformapi")){
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                    startActivity(intent);
                    return false;
                }else if(url.contains("weidianbuyer")){
                    return false;
                }else{
                    view.loadUrl(url);
                    return true;
                }
                //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
            }

            //页面开始加载
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }

            //加载结束 （其实页面404等等错误的情况也算加载完成）
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                if (dialog != null) {
                    dialog.dismiss();
                }
            }
        });
    }
    public static boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_BACK && webview.canGoBack()) {
            webview.goBack();// 返回前一个页面
            return true;
        }
        return true;
    }
}
